﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace ASmile.Extend
{
    public static class LinqExtend
    {
        /// <summary>
        /// 指定list列表是否有数据(为null可判断)
        /// </summary>
        public static bool HasData(this IList source)
        {
            return source != null && source.Count > 0;
        }
        /// <summary>
        /// 指定list列表是否没有数据(为null可判断)
        /// </summary>
        public static bool NoData(this IList source)
        {
            return !source.HasData();
        }
        /// <summary>
        /// 扩展distinct,去重复选择
        /// </summary>
        public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
        {
            HashSet<TKey> seenKeys = new HashSet<TKey>();
            foreach (TSource element in source)
            {
                if (seenKeys.Add(keySelector(element)))
                {
                    yield return element;
                }
            }
        }
        /// <summary>
        /// 将指定索引位置进行交换
        /// </summary>
        public static List<TSource> ExchangeIndex<TSource>(this List<TSource> rList, int srcIndex, int tarIndex)
        {
            var src = rList[srcIndex];
            var tar = rList[tarIndex];
            rList[tarIndex] = src;
            rList[srcIndex] = tar;
            return rList;
        }
        /// <summary>
        /// 返回最大值，无记录返回 0
        /// </summary>
        public static int MaxOrDefault<TSource>(this IEnumerable<TSource> source, Func<TSource, int> selector)
        {
            if (source == null || source.Count() == 0) return 0;
            return source.Max(selector);
        }
        /// <summary>
        /// 返回最大值，无记录返回 0
        /// </summary>
        public static int MaxOrDefault<TSource>(this IEnumerable<TSource> source, Func<TSource, int?> selector)
        {
            if (source == null || source.Count() == 0) return 0;
            var r = source.Max(selector);
            if (r == null) return 0;
            return r.Value;
        }
        /// <summary>
        /// 返回最大值，无记录返回 0
        /// </summary>
        public static decimal MaxOrDefault<TSource>(this IEnumerable<TSource> source, Func<TSource, decimal> selector)
        {
            if (source == null || source.Count() == 0) return 0;
            return source.Max(selector);
        }
        /// <summary>
        /// 表达式移除项
        /// </summary>
        public static bool Remove<TSource>(this List<TSource> source, Func<TSource, bool> predicate)
        {
            for (int i = source.Count - 1; i >= 0; i--)
            {
                if (predicate(source[i])) source.RemoveAt(i);
            }

            return true;
            //var newList = source.Where(predicate);
            //if (newList == null || newList.Count() == 0) return false;
            //for (int i = source.Count - 1; i >= 0; i--)
            //{
            //    var item = source[i];
            //    if (newList.Contains(item))
            //    {
            //        source.RemoveAt(i);
            //    }
            //}
            //return true;
        }
    }
}